<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!isFollowed"
    @click="onFollowed"
  >关注
  </van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollowed"
  >已关注
  </van-button>
</template>

<script>
import { addFollowUser, removeFollowUser } from '@/api/user'
import { Toast } from 'vant'

export default {
  name: 'FollowUser',
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    aut_id: {
      type: [Number, Object],
      required: true
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updateFollowed'
  },
  methods: {
    async onFollowed () {
      try {
        if (this.isFollowed) {
          await removeFollowUser(this.aut_id)
          console.log('取消关注')
        } else {
          console.log('关注')
          await addFollowUser(this.aut_id)
        }
        Toast.success('成功')
        // this.isFollowed = !this.is_followed
        this.$emit('updateFollowed', !this.isFollowed)
      } catch (e) {
        Toast.fail('网络异常')
      }
    }

  }
}
</script>

<style scoped lang='less'>

</style>
